import User from "../model/User";
import { userinfo, type asyncPromise } from "../interface";
import { cookieuserinfo } from "../type";
import md5 from "md5";
export default {
  async login(data: userinfo): asyncPromise {
    const result = await User.findOne({
      where: {
        name: data.name,
        pwd: data.pwd,
      },
    });
    if (result instanceof Object) return result.toJSON();
    return null;
  },
  async isname(name: string) {
    const is = await User.findOne({ where: { name: name } });
    if (is) return "用户名已经存在";
    return "用户名可以使用";
  },
  async registry(data: userinfo): asyncPromise {
    const is = await User.findOne({ where: { name: data.name } });
    if (is) return "用户名经存在";
    const result = await User.create(data as any);
    return result.toJSON();
  },
  async findistoken(token: { name: string; id: string }) {
    const result = await User.findOne({
      attributes: ["name", "imgUrl", "nickName", "id", "right"],
      where: {
        name: token.name,
        id: token.id,
      },
    });
    if (result instanceof Object) return result.toJSON();
    return null;
  },
  async userupdate(info: { nickName?: string; imgUrl?: string }, id: number) {
    const result = await User.update(info, { where: { id: id } });
    return result;
  },
  async getuserlist() {
    const result = await User.findAll({
      attributes: { exclude: ["pwd"] },
    });
    return result;
  },
  async changeright(id: number, data: { right: string }) {
    const [result] = await User.update(data, { where: { id } });
    return result;
  },
};
